package kr.jin.zeroprojectone;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;
import android.widget.Toast;

public class SettingPage extends PreferenceActivity {
	int mSelect = 0;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.layout.settingpage);
		ListPreference mainPage = (ListPreference)findPreference("mainsetting");
		mainPage.setOnPreferenceClickListener(new OnPreferenceClickListener() {
			
			@Override
			public boolean onPreferenceClick(Preference preference) {
				MainTime.ifif = true;
				return false;
			}
		});
		
		Preference resetData = (Preference) findPreference("reset");
		resetData.setOnPreferenceClickListener(new OnPreferenceClickListener() {
			String title = getResources().getString(R.string.class_settingpage_title);
			String confirm = getResources().getString(R.string.class_settingpage_confrim);
			String cancel = getResources().getString(R.string.class_settingpage_cancel);
			String toast1 = getResources().getString(R.string.class_settingpage_toast1);
			String toast2 = getResources().getString(R.string.class_settingpage_toast2);
			String toast3 = getResources().getString(R.string.class_settingpage_toast3);
			String toast4 = getResources().getString(R.string.class_settingpage_toast4);
			
			
			@Override
			public boolean onPreferenceClick(Preference preference) {
				new AlertDialog.Builder(SettingPage.this)
				.setTitle(title)
				.setSingleChoiceItems(R.array.entries_reset_data, -1, new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						mSelect = which;					}
				})
				.setPositiveButton(confirm, new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						DBHelper dbHelper= new DBHelper(SettingPage.this);
						SQLiteDatabase db = dbHelper.getWritableDatabase();
						switch(mSelect){
						case 0://1단계
							db.execSQL("DELETE FROM step1");
							Toast.makeText(SettingPage.this, toast1, Toast.LENGTH_SHORT).show();
							break;
						case 1://2단계
							db.execSQL("DELETE FROM step2");
							db.execSQL("DELETE FROM step3");
							Toast.makeText(SettingPage.this, toast2, Toast.LENGTH_SHORT).show();
							break;
						case 2://3단계
							db.execSQL("DELETE FROM step4");
							Toast.makeText(SettingPage.this, toast3, Toast.LENGTH_SHORT).show();
							break;
						case 3://4단계
							db.execSQL("DELETE FROM step6");
							Toast.makeText(SettingPage.this, toast4, Toast.LENGTH_SHORT).show();
							break;
						}
					}
				})
				.setNegativeButton(cancel, null)
				.show();
				return false;
			}
		});
	}
	
	
	
	
}
	
